using System;
using System.Collections.Generic;
using System.Linq;
using AfterlifeLib;
using AfterlifeLib.Lighting;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;
using Microsoft.Xna.Framework.Graphics;
// TODO: replace this with the type you want to write out.
using TWrite = AfterlifeLib.Levels.Level;

namespace ContentPipelineExtensionLibrary
{
    /// <summary>
    /// This class will be instantiated by the XNA Framework Content Pipeline
    /// to write the specified data type into binary .xnb format.
    ///
    /// This should be part of a Content Pipeline Extension Library project.
    /// </summary>
    [ContentTypeWriter]
    public class LevelTypeWriter : ContentTypeWriter<TWrite>
    {
        //Level file structure
        // - GameTilesetPath
        // - The amount of TileLayers
        // - All the TileLayers
        // - The only CollisionLayer
        // - The AmbientColor
        // - The amount of Lights
        // - All the Lights
        protected override void Write(ContentWriter output, TWrite value)
        {
            output.Write(value.GameTilesetPath);

            output.Write(value.TileLayers.Length);

            foreach (var tileLayer in value.TileLayers)
                tileLayer.WriteBinary(output);

            value.CollisionLayer.WriteBinary(output);

            output.Write(value.AmbientColor);

            output.Write(value.Lights.Count);
            foreach (Light light in value.Lights)
            {
                output.Write(light.Name);
                output.Write(light.Color);
                output.Write(light.Radius);
                output.Write(light.Strength);
                output.Write(light.Position);
            }
        }

        public override string GetRuntimeReader(TargetPlatform targetPlatform)
        {
            // TODO: change this to the name of your ContentTypeReader
            // class which will be used to load this data.
            return "AfterlifeLib.ContentTypeReaders.LevelTypeReader, AfterlifeLib";
        }
    }
}
